home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 101 / CD-ROM 101.iso / compl / maya5ple / Install_MayaPLE5_English.exe / Maya / Data1.cab / setWorkingDirectory.mel < prev    next >
Encoding:
Text File  |  2003-07-17  |  6.6 KB  |  258 lines

  1. // Copyright (C) 1997-2002 Alias|Wavefront,
  2. // a division of Silicon Graphics Limited.
  3. //
  4. // The information in this file is provided for the exclusive use of the
  5. // licensees of Alias|Wavefront.  Such users have the right to use, modify,
  6. // and incorporate this code into other products for purposes authorized
  7. // by the Alias|Wavefront license agreement, without fee.
  8. //
  9. // ALIAS|WAVEFRONT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  10. // INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
  11. // EVENT SHALL ALIAS|WAVEFRONT BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  12. // CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  13. // DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  14. // TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. // PERFORMANCE OF THIS SOFTWARE.
  16. //
  17. //
  18. //  Alias|Wavefront Script File
  19. //  MODIFY THIS AT YOUR OWN RISK
  20. //
  21. //  Creation Date:  December 9, 1996
  22. //  Author:         DSW
  23. //
  24. //  Description:
  25. //      sets the users working directory depending on the type of
  26. //        file requested and the users preset preferences.
  27. //
  28. //  Input Arguments:
  29. //      String      The workspace.
  30. //        String        The type of file.
  31. //        String        The object type (eg. scene, light, etc.)
  32. // 
  33. //  Return Value:
  34. //        The new working directory.
  35. //
  36.  
  37. proc string dirname(string $name)
  38. //
  39. //    Description:
  40. //        Given a file name, return the base name
  41. //
  42. {
  43.     string $fname = $name;
  44.  
  45.     if (`about -nt`) {
  46.         $fname = convert($fname);
  47.     }
  48.  
  49.     string $leading = match("^/", $fname);
  50.  
  51.     string $path[];
  52.     int $nTokens = tokenize($fname, "/", $path);
  53.  
  54.     string $dir = $leading;
  55.  
  56.     int $i;
  57.  
  58.     for ($i = 0; $i < $nTokens - 1; ++$i)
  59.     {
  60.         $dir = $dir + $path[$i] + "/";
  61.     }
  62.  
  63.     if ($dir == "")
  64.     {
  65.         $dir = ".";
  66.     }
  67.  
  68.     return $dir;
  69. }
  70.  
  71. global proc string setWorkingDirectory ( string $workspace, string $fileFormat, string $objectType )
  72. //
  73. //    Description:
  74. //        Given the file type, check the users preferences for this
  75. //        workspace and change to the specified directory. If an objectType
  76. //        is also specified, then also change to the subdirectory for
  77. //        object type.
  78. //
  79. //    Inputs:
  80. //        $workspace - the current project directory
  81. //        $fileFormat - mayaAscii, mayaBinary, etc.
  82. //        $objectType - the classification of the object, "shader", "texture/2d", etc
  83. //
  84. //    Returns:
  85. //        The path to the directory that we end up in.
  86. //
  87. //    Notes:
  88. //        The companion method for this is retainWorkingDirectory.
  89. //        This should be called from the browser callback.
  90. //
  91. {
  92.     string $pathSep = ":";
  93.     if (`about -nt`) {
  94.         $pathSep = ";";
  95.     }
  96.  
  97.     string $foundPath;
  98.     string $rootDir = `workspace -q -rd $workspace`;
  99.     string $defaultDir = `workspace -q -rd`;
  100.     global string $gDirRetainingOptionVar;
  101.     string $ruleList[];
  102.  
  103.     workspace -dir $rootDir $workspace;        // Change to the root first.
  104.  
  105.     if ($fileFormat == "Best Guess") {
  106.         // Use the default.
  107.         if (`about -evalVersion`) {
  108.             // Personal Learning Edition
  109.             //
  110.             $fileFormat = "mayaPLE";
  111.         } else {
  112.             // Standard Maya
  113.             //
  114.         $fileFormat = "mayaBinary";
  115.         }
  116.     } else if ($fileFormat == "image") {
  117.         if ($objectType == "" ||
  118.             $objectType == "scene")
  119.         {
  120.             $objectType = "images";
  121.         }
  122.     } else if (`about -evalVersion`) {
  123.         if ($fileFormat == "mayaBinary" || $fileFormat == "mayaAscii") {
  124.             // the personal Learning Edition does not support .ma or .mb
  125.             //
  126.             $fileFormat = "mayaPLE";
  127.         }
  128.     }
  129.  
  130.     string $optionVarName = "browserLocation"+$fileFormat+$objectType;
  131.  
  132.     $gDirRetainingOptionVar = $optionVarName;
  133.  
  134.     if ($fileFormat != "") {
  135.         string $fileLocations[] = `workspace -q -fr $workspace`;
  136.  
  137.         int $index;
  138.         int $listSize = size($fileLocations);
  139.         int $found = false;
  140.         string $ruleLocation;
  141.  
  142.  
  143.         if ( $fileFormat == "mayaAscii" || $fileFormat == "mayaBinary" || $fileFormat == "mayaPLE" )
  144.         {
  145.             if ($objectType == "") {
  146.                 $objectType = "scene";
  147.             }
  148.  
  149.             string $curObjectRules[] = `workspace -q -ot $workspace`;
  150.             $listSize = size($curObjectRules);
  151.  
  152.             for ($index = 0; $index < $listSize; $index+=2) {
  153.                 if ($curObjectRules[$index] == $objectType) {
  154.                     tokenize($curObjectRules[$index+1], $pathSep, $ruleList);
  155.                     if (size($ruleList) > 0) {
  156.                         $ruleLocation = $ruleList[0];
  157.                     }
  158.                     $found = true;
  159.                     break;
  160.                 }
  161.             }
  162.         } else if ($fileFormat == "image") {
  163.             string $curRenderRules[] = `workspace -q -rt $workspace`;
  164.  
  165.             int $listSize = size($curRenderRules);
  166.  
  167.             for ($index = 0; $index < $listSize; $index+=2) { 
  168.                 if ($curRenderRules[$index] == $objectType) {
  169.                     tokenize($curRenderRules[$index+1], $pathSep, $ruleList);
  170.                     if (size($ruleList) > 0) {
  171.                         $ruleLocation = $ruleList[0];
  172.                     }
  173.                     $found = true;
  174.                 }
  175.             }
  176.         } else {
  177.             for ($index = 0; $index < $listSize; $index+=2) {
  178.                 if ($fileLocations[$index] == $fileFormat) {
  179.                     tokenize($fileLocations[$index+1], $pathSep, $ruleList);
  180.                     if (size($ruleList) > 0) {
  181.                         $ruleLocation = $ruleList[0];
  182.                     }
  183.                     $found = true;
  184.                     break;
  185.                 }
  186.             }
  187.         }
  188.  
  189.         if (size($ruleLocation) > 0) {
  190.             if (match("^/", $ruleLocation) == "/") {
  191.                 // then this is an absolute path.
  192.                 $defaultDir = $ruleLocation;
  193.             } else if (`about -nt` && match("^[a-zA-Z]:", $ruleLocation) != "") {
  194.                 // this is an absolute path for windows
  195.                 $defaultDir = $ruleLocation;
  196.             } else {
  197.                 int $nChars = `size($defaultDir)`;
  198.                 string $lastChar = `substring $defaultDir $nChars $nChars`;
  199.  
  200.                 if ($lastChar == "/") {
  201.                     $defaultDir += $ruleLocation;
  202.                 } else {
  203.                     $defaultDir += ("/"+$ruleLocation);
  204.                 }
  205.             }
  206.         }
  207.     } 
  208.  
  209.     int $setDir = false;
  210.     string $currentDir = $defaultDir;
  211.  
  212.     if (`optionVar -exists $optionVarName`) {
  213.         // Set to this path.
  214.         $currentDir = `optionVar -q $optionVarName`;
  215.         if (`file -q -ex $currentDir`) {
  216.             if (!catch(`workspace -dir $currentDir`)) {
  217.                 $setDir = true;
  218.                 $defaultDir = $currentDir;
  219.             }
  220.         }
  221.     }
  222.  
  223.     while (!$setDir)
  224.     {
  225.         if (`file -q -ex $defaultDir`) {
  226.             if (!catch(`workspace -dir $defaultDir`))
  227.             {
  228.                 $setDir = true;
  229.                 break;
  230.             }
  231.             else
  232.             {
  233.                 if ($defaultDir == ".")
  234.                 {
  235.                     // The last resort failed.  There must be something
  236.                     // wrong with workspace -dir.  But we don't want to
  237.                     // stick the user in an infinite loop so exit anyway.
  238.                     //
  239.                     break;
  240.                 }
  241.             }
  242.         }
  243.  
  244.         $defaultDir = dirname($defaultDir);
  245.     }
  246.  
  247.     if ($currentDir != $defaultDir) {
  248.         warning ($currentDir + " is not a valid directory.  Using " +
  249.                  $defaultDir + " instead.");
  250.     }
  251.  
  252.     optionVar -sv $optionVarName $defaultDir;
  253.     workspace -dir $defaultDir;
  254.  
  255.     $foundPath = `workspace -q -dir $workspace`;
  256.     return $foundPath;
  257. }
  258.